<?php
namespace Admin\Controller;
use Common\Controller\AdminBaseController;
use Lib\MyTree;
class CategoryController extends AdminBaseController
{
    public function index()
    {
        //获取页page
        $pg = I('get.p/d',0);
        //判断page是否为空，如果为空则默认为1
        $pg = empty($pg) ? 1 : $pg;
        $word = I('post.word');
        //判断关键词是否为空
        $where = empty($word) ? '1=1': 'fd_article.title like "%'.$word.'%"';
        // 实例化User对象
        $info = M('category');
        // 进行分页数据查询 注意page方法的参数的前面部分是当前的页数使用 $_GET[p]获取
        $list =$info->field('id,category,pid')->page($pg.',2')->select();
        $this->assign('list',$list);// 赋值数据集
        $count      = $info->where($where)->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,2);// 实例化分页类 传入总记录数和每页显示的记录数
        $show       = $Page->show();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('title','分类列表');
        $this->display('up');
    }
    public function add()
    {
        if (IS_AJAX) {
            //获取角色中文名称
            $category = I('post.category','');
            if(empty($category)){
                $this->ajaxReturn(['state'=>1,'msg'=>'分类不能为空！']);
            }
            $pid = I('post.pc',0);
            if(M('category')->where(array('category'=>$category))->getField('id')){
                $this->ajaxReturn(['state'=>1,'msg'=>'此分类已经存在！']);
            }
            if(M('category')->add(['category'=>$category,'pid'=>$pid])){
                $this->ajaxReturn(['state'=>0,'msg'=>'添加分类成功！']);
            }
            $this->ajaxReturn(['state'=>1,'msg'=>'添加分类失败！']);
        }else{
            $category_info = M('category')->field('id,category,pid')->order('pid asc')->select();
            $apple = new MyTree();
            $tree = $apple->my_tree($category_info);
            $this->assign('tree', $tree);
            $this->display();
        }


    }
    public function edit()
    {

        if(IS_POST){
            //获取修改过后的分类ID
            $id = I('post.id',0);
            if(empty($id)){
                $this->ajaxReturn(['state'=>1,'msg'=>'分类ID不能为空!']);
            }
            //获取修改的分类标题
            $category= I('post.category','');
            if(empty($category)){
                $this->ajaxReturn(['state'=>1,'msg'=>'分类标题不能为空！']);
            }
            //执行SQL添加操作
            $article_info = M('category')->where(['id'=>$id])->setField(['category'=>$category]);
            //判断是否添加成功
            if($article_info){
                $this->ajaxReturn(['state' => 0,'msg'=>'修改分类成功']);
            }
            $this->ajaxReturn(['state'=>1,'msg'=>'修改分类失败！']);
        }else{
            //获取文章分类表中的分类信息
            $category_info = M('category')->field('id,category,pid')->order('pid asc')->select();
            //实例化无限级分类的类
            $apple = new MyTree();
            //获取分类结果集
            $tree = $apple->my_tree($category_info);
            //获取要修改的文章ID
            $id = I('get.id',0);
            //判断ID是否存在
            if(empty($id)){
                $this->ajaxReturn(['state'=>1,'msg'=>'分类ID不能为空！']);
            }
            //执行SQL操作，查出一条数据todo
            $category_info= M('category')->field('id,category,pid')->where(['id'=>$id])->find();
            //文章内容数据，传给前端
            $this->assign('category_info',$category_info);
            $this->assign('tree',$tree);
            //加载修改页面
            $this->display();
        }

    }
    public function del()
    {
        if(IS_POST){
            $id = I('post.id/d',0);
            if(empty($id)){
                $this->ajaxReturn(['state'=>1,'msg'=>'分类ID不能为空！']);
            }
            //执行SQL操作，判断是否成功
            if(!M('Category')->where(['id'=>$id])->getField('id')){
                $this->ajaxReturn(['state'=>1,'msg'=>'不能删除不存在的文章分类！']);
            }
            if(M('Category')->where(['pid'=>$id])->getField('id')){
                $this->ajaxReturn(['state'=>1,'msg'=>'不能删除存在孩子的文章分类！']);
            }
            M('Category')->startTrans();
            if(M('Category')->delete($id) == false){
                M('Category')->rollback();
                $this->ajaxReturn(['state' => 1, 'msg' => '删除失败！']);
            }
            if(M('Article')->where(['cate_id'=>$id])->delete() === false){
                M('Category')->rollback();
                $this->ajaxReturn(['state' => 1, 'msg' => '删除失败！']);
            }
            M('Category')->commit();
            $this->ajaxReturn(['state'=>0,'msg'=>'删除成功！']);
        }else{
            $this->ajaxReturn(['state' => 1, 'msg' => '非法操作！']);
        }
    }
}